package com.star.spring.ai.config;

import com.star.spring.ai.function.HotNewsFunction;
import com.star.spring.ai.service.LoggingAdvisor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallbackWrapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AIConfig {


    @Bean
    public FunctionCallback hotNewsFunctionInfo() {
        return FunctionCallbackWrapper.builder(new HotNewsFunction()).withName("hotNews")
                .withDescription("获取热点新闻")
                .build();
    }

    @Bean("chatClient")
    public ChatClient getChatClient(ChatClient.Builder chatClientBuilder) {
        return chatClientBuilder
                .defaultSystem("你是star航空公司的客户聊天代理，您正在通过在线聊天系统与客户互动，" +
                        "在提供有关预定或者取消预订的信息之前，首先得确保上下文中有以下信息：预定号、客户姓名，如果没有的话则您必须先从用户处获取" +
                        "请用中文回答客户的问题。今天的日期是{current_date}")
                .defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory()),
                        new LoggingAdvisor())
                .defaultFunctions("cancelBooking", "getBookingDetails")
                .build();
    }

    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }


}
